package com.cate.controller.admin;

import cn.hutool.log.Log;
import com.cate.pojo.dto.CategoryInfoDTO;
import com.cate.pojo.dto.CategoryPageDTO;
import com.cate.result.RestResponse;
import com.cate.service.CategoryService;
import com.cate.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @CreateTime: 2023/7/29
 * @Author: <==lhd==>
 * @Description:
 */
@RestController
@Slf4j
@RequestMapping("/admin/category")
@Api(tags = "分类管理")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @PutMapping
    @ApiOperation("修改分类")
    public RestResponse update(@Validated  @RequestBody CategoryInfoDTO categoryInfoDTO) {
        categoryService.update(categoryInfoDTO);
        return ResultUtil.success(null);
    }

    @GetMapping("/list")
    @ApiOperation("根据类型查询分类")
    public RestResponse list(
            @RequestParam("type")
            @Range(min = 1, max = 2, message = "type error") Integer type) {
        return ResultUtil.success(categoryService.listByType(type));
    }

    @DeleteMapping
    @ApiOperation("根据id删除分类")
    public RestResponse deleteById(@RequestParam("id") Long id) {
        categoryService.deleteById(id);
        return ResultUtil.success(null);
    }

    @PostMapping
    @ApiOperation("新增分类")
    public RestResponse save(@Validated @RequestBody CategoryInfoDTO categoryInfoDTO) {
        log.info(categoryInfoDTO.toString());
        categoryService.save(categoryInfoDTO);
        return ResultUtil.success(null);
    }

    @GetMapping("/page")
    @ApiOperation("分类分页查询")
    public RestResponse pageQuery(@Valid CategoryPageDTO categoryPageDTO) {
        return ResultUtil.success(categoryService.pageQuery(categoryPageDTO));
    }

    @PostMapping("/status/{status}")
    @ApiOperation("设置分类状态")
    public RestResponse setStatus(@PathVariable("status") Integer status, Long id) {
        categoryService.setStatus(id, status);
        return ResultUtil.success(null);
    }

}
